package com.lkd.job;

import com.lkd.common.VMSystem;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
@Slf4j
public class UserJob {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) throws Exception{

        try {
            XxlJobLogger.log("每日工单列表初始化");
            //todo:查询用户列表
            userService.list().forEach(u->{
                if (u.getRoleCode()!="1001"){
                    String key= VMSystem.REGION_TASK_KEY_PREF
                            + LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd"))
                            +u.getRegionId()+u.getRoleCode();
                    redisTemplate.opsForZSet().incrementScore(key,u.getId(),0);
                    redisTemplate.expire(key, Duration.ofDays(1));
                }
            });
            //todo:构建数据（zset）

            return ReturnT.SUCCESS;
        } catch (Exception e) {
            return ReturnT.FAIL;
        }
    }
}
